<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Proxy学习</title>
</head>
<body>

</body>
<script>
  /*var person = {
    name: "张三"
  };

  var proxy = new Proxy(person, {
    get: function(target, propKey) {
      if (propKey in target) {
        return target[propKey];
      } else {
        throw new ReferenceError("Prop name \"" + propKey + "\" does not exist.");
      }
    }
  });

  console.log(proxy.name) // "张三"
  console.log(proxy.age) // 抛出一个错误*/

  /*let proto = new Proxy({}, {
    get(target, propertyKey, receiver) {
      console.log('GET ' + propertyKey);
      return target[propertyKey];
    }
  });

  let obj = Object.create(proto);
  obj.foo // "GET foo"*/

  /*let validator = {
    set: function(obj, prop, value) {
      if (prop === 'age') {
        if (!Number.isInteger(value)) {
          throw new TypeError('The age is not an integer');
        }
        if (value > 200) {
          throw new RangeError('The age seems invalid');
        }
      }

      // 对于满足条件的 age 属性以及其他属性，直接保存
      obj[prop] = value;
    }
  };

  let person = new Proxy({}, validator);

  person.age = 100;

  console.log(person.age)// 100
  person.age = 'young' // 报错
  console.log(person.age)
  person.age = 300 // 报错
  console.log(person.age)*/

  /*var target = function () { return 'I am the target'; };
  var handler = {
    apply: function () {
      return 'I am the proxy';
    }
  };

  var p = new Proxy(target, handler);

  console.log(p());
  // "I am the proxy"*/



  var handler = {
    has (target, key) {
      if (key[0] === '_') {
        return false;
      }
      return key in target;
    }
  };
  var target = { _prop: 'foo', prop: 'foo' };
  var proxy = new Proxy(target, handler);
  console.log('_prop' in proxy) // false



</script>
</html>
